package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 商品修改控制器，数据回显后就可以修改了。
 */
@WebServlet(name = "ProductUpdateController",value = "/admin/updateProduct")
public class ProductUpdateController extends HttpServlet {
    private ProductService productService = null;
    public ProductUpdateController(){
        productService = new ProductService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //回显数据，在input里面给相应的values
        //接受请求参数
        //获取商品id
        String id = request.getParameter("id");
        String classifyId = request.getParameter("classify");
        String productName = request.getParameter("productName");
        //商品数量 int
        String productNumber = request.getParameter("productNumber");
        //商品价格 float
        String productPrice = request.getParameter("productPrice");
        //转型
        BigDecimal bigProductPrice = new BigDecimal(productPrice);
//        BigDecimal b = new BigDecimal(50.6);
        //商品图片
        String productPicture = request.getParameter("poster");
        //商品详情
        String productParticulars = request.getParameter("productParticulars");

        Product product = new Product();
        product.setProductId(Integer.parseInt(id));
        product.setClassifyId(Integer.parseInt(classifyId));
        product.setProductName(productName);
        product.setProductAllnumber(Integer.parseInt(productNumber));
        product.setProductPrice(bigProductPrice);
        product.setProductPicture(productPicture);
        product.setProductParticulars(productParticulars);

        productService.updateProduct(product);
        //跳转页面
//        response.sendRedirect(request.getContextPath()+"/admin/productList");
        //用重定向跳转才能把数据带回去，才能显示刚才用户输入流什么
        request.getRequestDispatcher(request.getContextPath()+"/admin/productListMerchant").forward(request,response);
    }
}
